/*
 * A JSR-168 portlet application for previewing unread
 * email messages.
 *
 * Copyright (C) 2007  San Joaquin Delta College
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */
package edu.deltacollege.portal.portlets.emailpreview;

import java.util.Comparator;

import javax.mail.Message;
import javax.mail.MessagingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A comparator for comparing the sent dates of
 * <code>javax.mail.Message</code> instances. The comparator can
 * be used for sorting <code>javax.mail.Message</code> instances by
 * their sent date.
 *
 * @author Andreas Christoforides
 *
 */
public class MessageComparator implements Comparator {

    public static final int ASC_ORDER = 1;
    public static final int DESC_ORDER = -1;
    public static final int DEFAULT_ORDER = ASC_ORDER;
	private Log log;
	private int order;

	public MessageComparator() {
		this(DEFAULT_ORDER);
	}
	
	public MessageComparator(int order) {
	    this.order = order;
        this.log = LogFactory.getLog(MessageComparator.class);
    }


	/**
	 * Compares two <code>javax.mail.Message</code> instances based on the dates
	 * returned by their {@link javax.mail.Message#getSentDate() getSentDate()} method.
	 * send
	 * @param arg1 First <code>javax.mail.Message</code> operand.
	 * @param arg2 Second <code>javax.mail.Message</code> operand.
	 * @return Returns -1, 1 and 0 if the sent date of the first message is
	 * before,after or equal with the send date of the second message respectively.
	 *
	 * @throws IllegalArgumentException An <code>IllegalArgumentException</code> is thrown
	 * if any of the operands are null or not of type <code>javax.mail.Message</code>.
	 */
	public int compare(Object arg1, Object arg2) {

		if (arg1 == null || !(arg1 instanceof Message)) {

			throw new IllegalArgumentException(
					"First argument must not be null and must be of type javax.mail.Message");
		}

		if (arg2 == null || !(arg2 instanceof Message)) {

			throw new IllegalArgumentException(
					"Second argument must not be null and must be of type javax.mail.Message");
		}

		int result = 0;

		Message message1 = (Message) arg1;
		Message message2 = (Message) arg2;

		try {
		    if (message1.getSentDate().after(message2.getSentDate())) {
		        result = 1;
		    } else if (message1.getSentDate().before(message2.getSentDate())) {
		        result = -1;
		    }
		} catch (MessagingException me) {
			/*
			 * Reporting the error in the logs but allowing the method to return
			 * 0.
			 */
			log.error("Error occured during message sorting", me);
		}

		return result * order;
	}
}
